﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reap.AzureResource.Service {
	using Reap.AzureResource.Data.Infrastructure;
	using Reap.AzureResource.Data.Repositories;
	using Reap.AzureResource.Model.Models;
	using Reap.Common.Runtime.Dependency;

	[Dependency(typeof(IResourceService),ComponentLifeStyle.InRequestScope,Key = "IResourceService")]
	public class ResourceService:IResourceService
	{
		private IResourceRepository repository;

		private IUnitOfWork unitOfWork;

		public ResourceService(IResourceRepository repository, IUnitOfWork unitOfWork)
		{
			this.repository = repository;
			this.unitOfWork = unitOfWork;
		}
		public Resource AddResource(Resource resource)
		{
			repository.Add(resource);
			unitOfWork.SaveChanges();
			return resource;
		}

		public IEnumerable<Resource> GetAllResources()
		{
			return repository.GetAll();
		}

		public void UpdateResource(Resource resource)
		{
			 repository.Update(resource);
			unitOfWork.SaveChanges();
		}
		public Resource GetResourceById(int id)
		{
			return repository.GetById(id);
		}

		public void SaveChages()
		{
			   unitOfWork.SaveChanges();
		}
	}

	public interface IResourceService {
		Resource AddResource(Resource resource);
		IEnumerable<Resource> GetAllResources();

		void UpdateResource(Resource resource);
		Resource GetResourceById(int id);
	}
}
